$(function () {
    console.log("get detail in ");
    getProductList(type);
    
    
});

function toProductionDetailPage(id){
    var productDetailPageUrl=basePath+"res/details_page.html?access_token="+access_token+"&pid=";
    window.location.href=productDetailPageUrl+id;
}

//ajax获取商品详情
function getProductList(type){
    //type:1富硒商品；2每日优礼；
    $.ajax({
        url:"/yangcangclub/v1/merchandise/list/show?access_token="+GetUrl("access_token")+"&type="+type+"&orderSort=0&pageSize=30",
        type:'GET',
        dataType:"json",
        success:function(data){
            if(data.code == 1000){
                data=data.data.list;
                if(data==undefined||data.length==0){return;}
                var html=""
                for(var i=0;i<data.length;i++){
                html+='<li><div class="txt"><div class="t-left"><div class="cont-img">'+
                '<a href="javascript:void(0);"><img src="'+data[i].bannerUrl+'"  onclick="toProductionDetailPage('+data[i].id+');"></a></div></div>'+
                '<div class="t-right"><div class="cont-tit "><div class="h3"  onclick="toProductionDetailPage('+data[i].id+');">'+
                '<p>'+data[i].name+'</p></div>'+
                '<p><span class="hour">羊仓价</span><span class="Price">￥'+data[i].unitPrice+'.00</span>' +
                    (data[i].showUnitPrice==0?"":
                    '<p class="yprice">原价<s>￥'+data[i].showUnitPrice+'.00</s></p>') +
                    '</p>'+
                '<button class="btn" onclick="addChart('+data[i].id+');"><p>立即购买</p></button></div></div></div></li>';
                }
                $(".ullist").html(html);
                
            }else{
                layer.open({content: data.msg, skin: 'msg', time: 2});
            }
        },
        error:function(data){
            layer.open({content: '请求失败，请刷新再试', skin: 'msg', time: 2});
        }
    });
    
}

function addChart(id){
    //添加到购物车。
    $.ajax({
        url:"/yangcangclub/v1/trolley/add?access_token="+access_token+"&pid="+id+"&num=1",
        type:'GET',
        dataType:"json",
        success:function(data){
            if(data.code == 1000){
                //成功后跳转到购物车页
                var productDetailPageUrl=basePath+"res/gwc.html?access_token="+access_token;
                window.location.href=productDetailPageUrl;

            }else{
                layer.open({content: data.msg, skin: 'msg', time: 2});
            }
        },
        error:function(data){
            layer.open({content: '请求失败，请刷新再试', skin: 'msg', time: 2});
        }
    });
}




